/*******************************************************************************
**
** Copyright (c) 2011 - The OWE Team
**
** This file is part of OWE.
**
** OWE is a free software; you can redistribute it and/or
** modify it under the terms of the GNU Lesser General Public
** License as published by the Free Software Foundation; version
** version 2.1 of the License.
**
** OWE is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
** Lesser General Public License for more details.
**
** You should have received a copy of the GNU Lesser General Public
** License along with OWE. If not, see <http://www.gnu.org/licenses/>.
**
*******************************************************************************/

#include <iostream>

#include <owe/authorelement.h>
#include <owe/contentelement.h>
#include <owe/configurationdocument.h>
#include <owe/descriptionelement.h>
#include <owe/featureelement.h>
#include <owe/iconelement.h>
#include <owe/licenseelement.h>
#include <owe/nameelement.h>
#include <owe/paramelement.h>
#include <owe/platform.h>
#include <owe/preferenceelement.h>
#include <owe/widgetelement.h>
#include <owe/widgetpackage.h>

OWE_USE_NAMESPACE

int main(int argc, char **argv)
{
    if (argc < 2 || argc > 3) {
        std::cout << "Usage: pkgdump <widget> [locale]" << std::endl;
        return 1;
    }

    const char *locale = "";
    if (argc == 3)
        locale = argv[2];

    Platform(argc, argv);
    WidgetPackage package(argv[1]);

    if (!package.isValid()) {
        std::cout << "Widget package " << argv[1] << " is invalid." << std::endl;
        return 1;
    }

    std::cout << "Widget package " << argv[1] << " is valid." << std::endl;

    ConfigurationDocument configuration(package);

    if (!configuration.isValid()) {
        std::cout << "Widget configuration " << argv[1] << " is invalid." << std::endl;
        return 1;
    }

    std::cout << "Widget configuration " << argv[1] << " is valid." << std::endl;
    const WidgetElement *widget = configuration.widgetElement();

    // Dumping widget element
    std::cout << "Widget element:" << std::endl;
    std::cout << "# id: " << widget->id() << std::endl;
    std::cout << "# version: " << widget->version() << std::endl;
    std::cout << "# height: " << widget->height() << std::endl;
    std::cout << "# width: " << widget->width() << std::endl;
    std::cout << "# viewmodes: " << widget->viewModes() << std::endl;

    const NameElement *name = widget->name(locale);
    if (name) {
        std::cout << "# name (short): " << name->short_() << std::endl;
        std::cout << "# name: " << name << std::endl;
    }

    const DescriptionElement *description = widget->description(locale);
    if (description)
        std::cout << "# description: " << description << std::endl;

    const AuthorElement *author = widget->author();
    if (author) {
        std::cout << "# author: " << author->text() << std::endl;
        std::cout << "# author email: " << author->email() << std::endl;
        std::cout << "# author href: " << author->href() << std::endl;
    }

    const FeatureElementList feature = widget->feature();
    FeatureElementList::const_iterator featureIt = feature.begin();
    while (featureIt != feature.end()) {
        std::cout << "# feature: " << featureIt->second->name() << std::endl;
        std::cout << "# feature required: " << featureIt->second->required() << std::endl;

        const ParamElementList param = featureIt->second->param();
        ParamElementList::const_iterator paramIt = param.begin();

        while (paramIt != param.end()) {
            std::cout << "#   param: " << paramIt->second->name() << std::endl;
            std::cout << "#   param value: " << paramIt->second->value() << std::endl;
            paramIt++;
        }
        featureIt++;
    }

    const LicenseElement *license = widget->license();
    if (license) {
        std::cout << "# license: " << license << std::endl;
        std::cout << "# license href: " << license->href() << std::endl;
    }

    const ContentElement *content = widget->content();
    if (content) {
        std::cout << "# content src: " << content->src() << std::endl;
        std::cout << "# content encoding: " << content->encoding() << std::endl;
        std::cout << "# content type: " << content->type() << std::endl;
    }

    const IconElementList icon = widget->icon();
    IconElementList::const_iterator iconIt = icon.begin();
    while (iconIt != icon.end()) {
        std::cout << "# icon src: " << iconIt->second->src() << std::endl;
        std::cout << "# icon width: " << iconIt->second->width() << std::endl;
        std::cout << "# icon height: " << iconIt->second->height() << std::endl;
        iconIt++;
    }

    const PreferenceElementList preference = widget->preference();
    PreferenceElementList::const_iterator preferenceIt = preference.begin();
    while (preferenceIt != preference.end()) {
        std::cout << "# preference: " << preferenceIt->second->name() << std::endl;
        std::cout << "# preference value: " << preferenceIt->second->value() << std::endl;
        std::cout << "# preference read only: " << preferenceIt->second->readOnly() << std::endl;
        preferenceIt++;
    }

    return 0;
}
